package Lint;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Enumeration;

public class LoginServlet extends HttpServlet {
    private ServletContext context = null;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("/login.jsp").forward(request,response);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        String lname = request.getParameter("name");    //获取登陆页面输入框内的内容
        String lpw = request.getParameter("password");
        String driverName = "com.mysql.cj.jdbc.Driver";     //调用数据库
        String dbURL="jdbc:mysql://localhost:3306/user?serverTimezone=GMT%2B8";
        String userName = "root";
        String userPwd = "63c16874";

        try {
            Class.forName(driverName);
            Connection con= DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("连接数据库成功");
            Statement statement = con.createStatement();
            String sql = "select * from login;";
            ResultSet resultSet = statement.executeQuery(sql);
            String name=" ";
            String pw=" ";
            int i=1;
            while (resultSet.next()) {
                name = resultSet.getString("username");     //获取数据库中所有用户信息
                pw = resultSet.getString("userpassword");
                User u=new User(i,name,pw);
                if(name.equals(lname)){
                    break;
                }
                i++;
            }
            System.out.println(resultSet.getRow());
            if(resultSet.getRow()==0){
                System.out.println("用户名不存在");
                request.setAttribute("result", "用户名"+lname+"不存在");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }
            else if(pw.equals(lpw))
            {
                System.out.println("登录成功"+"用户名"+lname);
                request.getSession().setAttribute("user", lname);
                context = request.getSession().getServletContext();
                if (context.getAttribute("loginUsers")!=null){
                    context.setAttribute("loginUsers",(Integer)context.getAttribute("loginUsers")+1);
                }
                else {
                    context.setAttribute("loginUsers", 1);
                }
//                request.getRequestDispatcher("/login").forward(request,response);
                response.sendRedirect("/loginsuccess");     //登陆成功后跳转到loginsuccess页面

            }
            else
            {
                System.out.println("登录失败");
                request.setAttribute("result", "登录失败，密码"+lpw);
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.print("连接失败");
        }


    }
}
